package com.xiaolin.oa.utils;

import com.xiaolin.oa.enums.StatusCode;
import lombok.Data;

/**
 * @author xiaolin
 * @description 描述
 * @since 2024/7/18
 */
@Data
public class OptResult<T> {
    private int code;
    private String msg;
    private String desc;
    private T data;

    public OptResult(int code, String msg, String desc, T data) {
        this.code = code;
        this.msg = msg;
        this.desc = desc;
        this.data = data;
    }

    public OptResult(StatusCode statusCode, String desc, T data) {
        this.code = statusCode.getCode();
        this.msg = statusCode.getMsg();
        this.desc = desc;
        this.data = data;
    }

    public static OptResult ok() {
        return new OptResult<>(StatusCode.OK, "", null);
    }

    public static <T> OptResult<T> ok(T data) {
        return new OptResult<>(StatusCode.OK, "", data);
    }

    public static OptResult fail() {
        return new OptResult<>(StatusCode.FAIL, "", null);
    }

    public static OptResult fail(String desc) {
        return new OptResult<>(StatusCode.FAIL, desc, null);
    }

    public static OptResult fail(StatusCode statusCode, String desc) {
        return new OptResult<>(statusCode, desc, null);
    }

    public static OptResult optFlag(boolean isOk) {
        return isOk ? ok() : fail();
    }
}
